﻿using SienSchoofsProject.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SienSchoofsProject.Controllers
{
    public class FilmdbController : BaseController
    {
        private FilmDb db = new FilmDb();

        //
        // GET: /Filmdb/

        public ActionResult Index()
        {
            try
            {
                return View(db.Films.ToList());
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // GET: /Filmdb/Create

        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            try
            {
                return View();
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Filmdb/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "admin")]
        public ActionResult Create(Film film)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    db.Films.Add(film);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(film);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // GET: /Filmdb/Edit/5

        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id = 0)
        {
            try
            {

                Film film = db.Films.Find(id);
                if (film == null)
                {
                    return HttpNotFound();
                }
                return View(film);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Filmdb/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(Film film)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(film).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(film);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // GET: /Filmdb/Delete/5

        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            try
            {
                Film film = db.Films.Find(id);
                if (film == null)
                {
                    return HttpNotFound();
                }
                return View(film);
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        //
        // POST: /Filmdb/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "admin")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Film film = db.Films.Find(id);
                db.Films.Remove(film);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/Logging/Log.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException:\n" + e.StackTrace);
                }
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
